ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I use the command egrep '*dasdxas*' FILE
to display the line with dasdxas.
now i need the first part before the comma. And put that in a variable
and I need the third one. and put that one in another variable.
and this with all line that include dasdxas
Does someone know how to do that?
and another problem is that the file is binary and i have to manual save it as text. is there a bash command or script to convert binary file to text file?
You should indicate which value you want into variables. The third entry is dasxdas itself. Regular expressions are very exacting, so your description needs to be exact. You are clear on the input but not on the results you want. It seems from your description that you want the 1st and 3rd field but you already know the 3rd, so that can't be what you meant.
Your regex input to egrep is wrong. You are using filesystem type wildcards. The asterisk character needs to be preceded by a character or regular expression:
egrep '.*dasdxas.*' FILE
or simply egrep 'dasdxas' FILE
You can extract a part of the input using awk or grep:
var1=$(sed -n '/dasdxas/s/^\([[:alnum:]][[:alnum:]]*\),.*/\1/p;/' FILE)
Because of the structure of the input, awk would work better:
You can fill an array with the notation: name=(value value value ...)
So have your awk command list just the values. Perhaps use printf instead of print so that
the results are output on the same line for all the records.
eval name=( $(awk ' ... { printf "%s %s ", $1,$4"}') )
For a file with 10 records, this will create an array variable filled with the values from
the first and forth field of all the records. Because I didn't use "\n" in the format for
printf, they all appear on the same line of the output.
For a very large file, this could cause a problem with too many arguments, or not enough
memory for the array variable to contain all the elements.
Eval is usually used when you dynamically construct a command and its arguments.
In the last example, I was editing my first example and didn't need it because only the argument was added. In my previous example, the awk command returned the LFS (left-hand-side) as well. When you do that, you need to use eval.
Here is a common usage of eval, if you use ssh and don't want to retype the passphrase every time in a session:
eval $(ssh-agent)
ssh-add
The ssh-agent command returns with three or four assignment statements, so the eval is needed.
There are quite a few regular expressions that will match.
Code:
/sbin/ifconfig | sed -rn '/([[:xdigit:]][[:xdigit:]]:){5}/p'
eth0 Link encap:Ethernet HWaddr 00:0F:B0:0C:EF:AB
eth1 Link encap:Ethernet HWaddr 00:90:4B:92:71:A1
eth2 Link encap:Ethernet HWaddr 00:04:5A:9D:D6:E8
jschiwal@hpamd64:~/work/apue/chapter_4> /sbin/ifconfig | sed -rn '/([[:xdigit:]][[:xdigit:]]:){5}/p'
I used the -r for extended regular expressions.
If I wanted to be more exact, I would have used
/([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]]/
The a sequence like 00:1b:20:45:56 wouldn't produce a false positive. If you are producing a generalized sed script to use in a program, you might want to be more exact than if you are working interactively in the shell.
---
Oops, you want two of them.
/([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]].*([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]]/
How can i convert a file from binary to text code?
Code:
od -tc file.bin > file.txt
od is "octal dump". Useful for finding embedded NULLs and other stuff in your files. See the manual page for options which produce alternative output formats.
/([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]].*([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]]/
in awk? or do i have to at parameters?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.